
// Define SudokuUI object if it is not already defined
if (typeof SudokuUI === 'undefined') var SudokuUI = {};

SudokuUI.table = 
{
	// list of DOM objects for fast access
	cells : [[], [], [], [], [], [], [], [], []],
	rowTally : [],
	colTally : [],

	// loop thru all the cells, add cell IDs, hook cell Peers event functions, 
	init : function ()
	{
		// create <table> element
		var objTable = document.createElement('table');
		objTable.id = 'sudoku_grid';

		for (var i = 0; i < 9; i++)
		{
			objTable.insertRow(i);
			for (var j = 0; j < 9; j++)
			{
				// create a cell in standard 9x9 area
				SudokuUI.table.cells[i][j] = objTable.rows[i].insertCell(j);
				SudokuUI.table.cells[i][j].appendChild(document.createTextNode('\u00A0'));

				// create new node attribute because its faster than using ID.
				SudokuUI.table.cells[i][j].r = i;
				SudokuUI.table.cells[i][j].c = j;

				SudokuUI.table.cells[i][j].onmouseover = SudokuUI.input.mousein_handler;
				SudokuUI.table.cells[i][j].onmouseout = SudokuUI.input.mouseout_handler;
			}
		}

		objTable.insertRow(9);
		for (var i = 0; i < 9; i++)
		{
			// create row tally
			SudokuUI.table.rowTally[i] = objTable.rows[i].appendChild(document.createElement('th'));
			SudokuUI.table.rowTally[i].appendChild(document.createTextNode('\u00A0'));

			// create column tally
			SudokuUI.table.colTally[i] = objTable.rows[9].appendChild(document.createElement('th'));
			SudokuUI.table.colTally[i].appendChild(document.createTextNode('\u00A0'));

			// style vertical box dividers
			objTable.rows[i].cells[0].style.borderLeft = '3px solid black';
			objTable.rows[i].cells[2].style.borderRight = '3px solid black';
			objTable.rows[i].cells[5].style.borderRight = '3px solid black';
			objTable.rows[i].cells[8].style.borderRight = '3px solid black';

			// style horizontal box dividers
			objTable.rows[0].cells[i].style.borderTop = '3px solid black';
			objTable.rows[2].cells[i].style.borderBottom = '3px solid black';
			objTable.rows[5].cells[i].style.borderBottom = '3px solid black';
			objTable.rows[8].cells[i].style.borderBottom = '3px solid black';
		}

		// insert it into the DOM
		var InsertLocation = document.getElementById('midcolumn');
		InsertLocation.insertBefore(objTable, InsertLocation.firstChild);
	},

	// clue or filled cell
	setCellValue : function (row, col, value)
	{
		var NewNode = document.createElement('DIV');
		NewNode.className = 'filled_content';
		NewNode.appendChild(document.createTextNode(value.bitmaskToDigit()));

		var CellObject = SudokuUI.table.cells[row][col];
		CellObject.className = '';
		CellObject.replaceChild(NewNode, CellObject.firstChild);
	},

	// notation cell
	setCellNotation : function (row, col, value)
	{
		if (value)
		{
			var NewNode = SudokuUI.table.createNotationDOM(value);
		}
		else
		{
			var NewNode = document.createElement('DIV');
			NewNode.className = 'blank_content';
			NewNode.appendChild(document.createTextNode('0'));
		}

		var CellObject = SudokuUI.table.cells[row][col];
		CellObject.className = '';
		CellObject.replaceChild(NewNode, CellObject.firstChild);
	},

	// blank cell
	setCellBlank : function (row, col)
	{
		var NewNode = document.createElement('DIV');
		NewNode.className = 'blank_content';
		NewNode.appendChild(document.createTextNode('0'));

		var CellObject = SudokuUI.table.cells[row][col];
		CellObject.className = '';
		CellObject.replaceChild(NewNode, CellObject.firstChild);
	},

	// add a CSS class to the cell
	applyStyle : function (row, col, className)
	{
		var CellObject = SudokuUI.table.cells[row][col];

		if (CellObject.className)
			CellObject.className += ' ' + className;
		else
			CellObject.className = className;
	},

	// remove all CSS class
	clearStyle : function (row, col)
	{
		SudokuUI.table.cells[row][col].className = '';
	},

	// converts a bitvalue representation to DOM structure.
	createNotationDOM : function (value)
	{
		var mask = 1;
		var DivNode = document.createElement('DIV');
		DivNode.className = 'notation_content';

		for (var i = 1; i < 10; i++)
		{
			if (value & mask) 
			{
				DivNode.appendChild(document.createTextNode(i));
			}
			else
			{
				var SpanNode = document.createElement('SPAN');
				SpanNode.style.visibility = 'hidden';
				SpanNode.appendChild(document.createTextNode(i));

				DivNode.appendChild(SpanNode);
			}
			if (i === 3 || i === 6) DivNode.appendChild(document.createElement('BR'));
			else if (i !== 9) DivNode.appendChild(document.createTextNode('\u00A0'));
			mask <<= 1;
		}
		return DivNode;
	},

	// update the tally notations on the side. 
	setRowTally : function (row, value)
	{
		var CellNode = SudokuUI.table.rowTally[row];
		if (value)
		{
			CellNode.replaceChild(SudokuUI.table.createNotationDOM(value), CellNode.firstChild);
		}
		else
		{
			CellNode.replaceChild(document.createTextNode('\u00A0'), CellNode.firstChild);
		}
	},

	// update the tally notations on the bottom. 
	setColTally : function (col, value)
	{
		var CellNode = SudokuUI.table.colTally[col];
		if (value)
		{
			CellNode.replaceChild(SudokuUI.table.createNotationDOM(value), CellNode.firstChild);
		}
		else
		{
			CellNode.replaceChild(document.createTextNode('\u00A0'), CellNode.firstChild);
		}
	},
	
	// hide the tally notations on the side and the bottom. 
	hideAllTallies : function ()
	{
		for (var i = 0; i < 9; i++)
		{
			SudokuUI.table.rowTally[i].replaceChild(document.createTextNode('\u00A0'), SudokuUI.table.rowTally[i].firstChild);
			SudokuUI.table.colTally[i].replaceChild(document.createTextNode('\u00A0'), SudokuUI.table.colTally[i].firstChild);
		}
	}
}